# R script for processing updated/extended data for U. of Vienna
# This file returns summary statistics for new Vienna performance measures

rm(list = ls())

if (!require("tidyverse")) install.packages("tidyverse")
if (!require("readxl")) install.packages("readxl")
if (!require("writexl")) install.packages("writexl")

# read faculty-level data ----

vienna1_falc <- readxl::read_xlsx("Dataset_extra_vienna.xlsx", sheet = "Vienna I - PoliSci") 
vienna2_falc <- readxl::read_xlsx("Dataset_extra_vienna.xlsx", sheet = "Vienna II - Government") 

vienna_falc <- rbind.data.frame(
  vienna1_falc |> dplyr::mutate(department = 'political science'), 
  vienna2_falc |> dplyr::mutate(department = 'government')
)

# create department-level data ----

vienna_dept = vienna_falc |>
  dplyr::summarise(
    dept_size = sum(status,na.rm = TRUE),
    across(.cols= citations:pub_top_recent_historic_ca, .fns = ~sum(.x, na.rm = TRUE))
  ) |>
  dplyr::ungroup() |>
  dplyr::mutate(across( 
    .cols = citations:pub_top_recent_historic_ca, 
    .fns = function(x) x/dept_size,
    .names = "{.col}_pf"
  ))

# create rank-based data ----

vienna2_metricbystatus_full <- rbind.data.frame(
  vienna1_falc |>
    dplyr::filter(rank=='full') |>
    dplyr::select(name, rank, status, citations:pub_top_recent),
  vienna2_falc |>
    dplyr::filter(rank=='full') |>
    dplyr::select(name, rank, status, citations:pub_top_recent)
) |>
  dplyr::summarise(
    dept_size = sum(status,na.rm = TRUE),
    across(.cols= citations:pub_top_recent, .fns = ~sum(.x, na.rm = TRUE))
  ) |>
  dplyr::mutate(across( 
    .cols = citations:pub_top_recent, 
    .fns = function(x) x/dept_size,
    .names = "{.col}_pf"
  ))

vienna2_metricbystatus_assoc <- rbind.data.frame(
  vienna1_falc |>
    dplyr::filter(rank=='associate') |>
    dplyr::select(name, rank, status, citations:pub_top_recent),
  vienna2_falc |>
    dplyr::filter(rank=='associate') |>
    dplyr::select(name, rank, status, citations:pub_top_recent)
) |>
  dplyr::summarise(
    dept_size = sum(status,na.rm = TRUE),
    across(.cols= citations:pub_top_recent, .fns = ~sum(.x, na.rm = TRUE))
  ) |>
  dplyr::mutate(across( 
    .cols = citations:pub_top_recent, 
    .fns = function(x) x/dept_size,
    .names = "{.col}_pf"
  ))

# There are no asisstant profs from the new school so no new data are created for this group

# TABLE 1: department totals ----

vienna_dept |>
  dplyr::select(
    dept_size,
    citations, impact, pub_top,
    citations_recent, impact_recent, pub_top_recent
  ) -> vienna_dept_totals

print(vienna_dept_totals)
# dept_size citations impact pub_top citations_recent impact_recent pub_top_recent
#   38     41820  1176.      14            11144          786.              8

# TABLE 2: faculty averages ----

vienna_dept |>
  dplyr::select(
    dept_size,
    citations_pf, impact_pf, pub_top_pf,
    citations_recent_pf, impact_recent_pf, pub_top_recent_pf
  ) -> vienna_dept_pfs

print(vienna_dept_pfs)
# dept_size citations_pf impact_pf pub_top_pf citations_recent_pf impact_recent_pf pub_top_recent_pf
#   38        1101.      30.9      0.368                293.             20.7             0.211

# TABLE 3: individual scholars ----

# Check new faculty from the government school only

vienna_falc |>
  dplyr::filter(department=='government') |>
  dplyr::select(
    name, 
    citations, impact, pub_top,
    citations_recent, impact_recent, pub_top_recent
  ) -> vienna_falc_govschool

print(vienna_falc_govschool)
# name                       citations impact pub_top citations_recent impact_recent pub_top_recent
# Carolina Plescia                 990  102.        0              798          89.9              0
# Christopher Wratil               970   79.8       7              361          57.1              4
# Laurenz Ennser- Jedenastik      2173   86.3       1              327          38.5              0
# Markus Wagner                   4824  159.        3             1551          87.3              3
# Sylvia Kritzinger               2280   64.5       0              682          48.6              0
# Thomas Meyer                    1782   78.1       0              357          47.2              0

# TABLE A4: department totals weighted by historical impact + coauthors ----

vienna_dept |>
  dplyr::select(
    dept_size,
    citations_historic_ca, impact_historic_ca, pub_top_historic_ca,
    citations_recent_historic_ca, impact_recent_historic_ca, pub_top_recent_historic_ca
  ) -> vienna_dept_historicca

print(vienna_dept_historicca)
# dept_size citations_historic_ca impact_historic_ca pub_top_historic_ca citations_recent_historic_ca impact_recent_historic…¹ pub_top_recent_histo…²
#   38                20730.               558.                28.2                        9595.                     351.                   18.5

# TABLE A5: faculty averages weighted by historical impact + coauthors ----

vienna_dept |>
  dplyr::select(
    dept_size,
    citations_historic_ca_pf, impact_historic_ca_pf, pub_top_historic_ca_pf,
    citations_recent_historic_ca_pf, impact_recent_historic_ca_pf, pub_top_recent_historic_ca_pf
  ) -> vienna_dept_historicca_pfs

print(vienna_dept_historicca_pfs)
# dept_size citations_historic_ca…¹ impact_historic_ca_pf pub_top_historic_ca_pf citations_recent_his…² impact_recent_histor…³ pub_top_recent_histo…⁴
#   38                    546.                  14.7                  0.743                   252.                   9.23                  0.486

# TABLE A6: department totals for full professors ----

vienna2_metricbystatus_full |>
  dplyr::select(
    dept_size, 
    citations, impact, pub_top,
    citations_recent, impact_recent, pub_top_recent
  ) -> vienna_falc_full2

print(vienna_falc_full2)
# dept_size citations impact pub_top citations_recent impact_recent pub_top_recent
#   11     27555   699.       7             7445          408.              4

# TABLE A7: department totals for associate professors ----

vienna2_metricbystatus_assoc |>
  dplyr::select(
    dept_size, 
    citations, impact, pub_top,
    citations_recent, impact_recent, pub_top_recent
  ) -> vienna_falc_assoc2

print(vienna_falc_assoc2)
# dept_size citations impact pub_top citations_recent impact_recent pub_top_recent
#   13.5     10376   300.       7             2635          221.              4

# TABLE A9: faculty averages for full professors ----

vienna2_metricbystatus_full |>
  dplyr::select(
    dept_size, 
    citations_pf, impact_pf, pub_top_pf,
    citations_recent_pf, impact_recent_pf, pub_top_recent_pf
  ) -> vienna_falc_full_pf2

print(vienna_falc_full_pf2)
# dept_size citations_pf impact_pf pub_top_pf citations_recent_pf impact_recent_pf pub_top_recent_pf
#   11         2505      63.6      0.636                677.             37.1             0.364

# TABLE A10: faculty averages for associate professors ----

vienna2_metricbystatus_assoc |>
  dplyr::select(
    dept_size, 
    citations_pf, impact_pf, pub_top_pf,
    citations_recent_pf, impact_recent_pf, pub_top_recent_pf
  ) -> vienna_falc_assoc_pf2

print(vienna2_metricbystatus_assoc)
# dept_size citations citations_recent impact impact_recent pub_top pub_top_recent citations_pf citations_recent_pf impact_pf impact_recent_pf
#   13.5     10376             2635   300.          221.       7              4         769.                195.      22.2             16.4

